<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
    });
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="CSS/Tree.css"/>
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css"/>
<script type="text/javascript" src="lib/datatables/js/jquery.dataTables.js"></script>
<!--<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable( {
"sPaginationType": "full_numbers"
} );
} );
</script> -->
<!-- DATATABLES CSS END -->
<link rel="stylesheet" type="text/css" href="${ctx}/css/tipswindown.css"/>
<script type="text/javascript" src="${ctx}/js/tipswindown.js"></script>

<h1 class="page-title">账号设置 - 添加</h1>

<div class="container_12 clearfix leading">

    <div class="side grid_12 leading">
        <form class="form has-validation" name="user_form" id="user_form" method="post">

            <div class="clear"></div>
            <div class="clearfix">

                <label for="employ_choose_name" class="form-label">选择员工</label>

                <div class="form-input"><span id="employ_choose_name">${name}</span>
                    <a class="button" style=" margin-top:10px" href="javascript:void(0)" onclick="onShowDiv()"><span
                            style="background:url(${ctx}/images/img/zoom.png)" class="icon">&nbsp;</span>选择</a>
                </div>

                <input type="hidden" id="base_id" name="user.employeeinfoBase.id" value="${id}">

            </div>

            <div class="clearfix">

                <label for="loginName" class="form-label">账号 <em>*</em></label>

                <div class="form-input"><input type="text" id="loginName" name="user.loginName" onblur="checkAccount(this)" required="required"
                                               maxlength="12" placeholder="字母或者数字 (最大12个字符)"/></div>


            </div>

            <div class="clearfix">

                <label for="password" class="form-label">密码</label>

                <div class="form-input"><input type="password" id="password" name="user.password" maxlength="30"
                                               placeholder="最大30个字符"/></div>

            </div>

            <div class="clearfix">

                <label for="repassword" class="form-label">密码确认</label>

                <div class="form-input"><input type="password" id="repassword" name="check"
                                               maxlength="30" placeholder="再次输入密码"/></div>

            </div>

            <div class="form-action clearfix">

                <input type="hidden" name="organizationId" value="${organizationId}">
                <button class="button" type="button" onclick="checkForm()">添加</button>

                <button class="button" type="reset">重置</button>
                <a class="button" href="#user/setup.xhtml"><span
                        style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>

            </div>
            <input type="hidden" id="user_name" name="user.name">
            <input type="hidden" id="work_choose_id" name="user.work.id">
        </form>
    </div>
</div>

<div class="clear"></div>


<script type="text/javascript" src="js/Tree.js"></script>
<script type="text/javascript">
    function checkForm(){
        
        if(checkNull($("#employ_choose_name").html())){
            alert("请选择员工");
            return false;
        }

        if(checkNull($("#loginName").attr("value"))){
            alert("账号不能为空");
            $("#loginName").focus();
            return false;
        }
        if(checkNull($("#password").attr("value"))){
            alert("密码不能为空");
            return false;
        }
        if(checkNull($("#repassword").attr("value"))){
            alert("确认密码不能为空");
            return false;
        }
        
        if($("#password").attr("value")!=$("#repassword").attr("value")){
            alert("两次密码不一致");
            return false;
        }

        var url = "${ctx}/user/user_create.xhtml";
        $.ajax(url, {
            type: "POST",
            data: $("#user_form").serialize(),
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url)
            },
            complete:function() {}
        });
    }


    function onShowDiv(){
        //$("#config-popover").css("width", 900);
        tipsWindown("选择员工","url:post?${ctx}/employee/staff_list.xhtml?operation=select&organizationId=${organizationId}","900","350","true","","true","text");
    }
    //员工选择框返回事件
    function onSelectEployee(eid, ename, wid){
        //alert(eid+"----------"+ename+"----------"+wid);
        if(eid == undefined || ename == undefined || wid == undefined){
            return;
        }
        $.ajax({
            type: 'POST',
            url:"${ctx}/ajax/check_user_work.xhtml",
            data:{"workId":wid},
            dataType:"json",
            success: function(exist){
                if(exist == true || exist == "true"){
                    alert("该员工已添加账号");
                    return;
                }else{
                    $("#work_choose_id").val(wid);
                    $("#employ_choose_name").html(ename);
                    $("#user_name").val(ename);
                    return;
                }
            }
        });
    }

    function checkAccount(obj){
        if(obj.value.length > 0){
            $.ajax({
                type: 'POST',
                url:"${ctx}/ajax/check_user_account.xhtml",
                data:{"account":obj.value},
                dataType:"json",
                success: function(exist){
                    if(exist == true || exist == "true"){
                        alert("该账号已存在");
                        $(obj).val("");
                        return;
                    }
                }
            });
        }
    }
</script>
    